package com.zl.service.impl;

import com.zl.pojo.Emp;
import com.zl.service.EmpUploadService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class EmpUploadServiceImpl implements EmpUploadService {
    @Override
    public Emp upload(Emp emp, CommonsMultipartFile userImg) throws IOException {
        //先拿出原始文件名称
        String oldName = userImg.getOriginalFilename();
        if(oldName != null && !"".equals(oldName)){
            //创建新文件名称(后缀是从旧文件名称中截取出来的)
            String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
            /*创建该文件对应的硬盘上的文件*/
            File newFile = new File("C:/static/empMaven/img/" + newName);
            //把上传框数据写到硬盘上的对应文件中
            userImg.transferTo(newFile);
            emp.setImg("img/" + newName);
        }
        return emp;
    }
}

